package com.wanfajie.smsdistribute.db.repository.impl

import com.j256.ormlite.dao.Dao
import com.wanfajie.smsdistribute.db.repository.TemplateRepository
import com.wanfajie.smsdistribute.db.bean.template.TemplateBean
import com.wanfajie.smsdistribute.db.helper.TemplateDatabaseHelper

object TemplateRepositoryImpl: TemplateRepository {

    private val templateDao: Dao<TemplateBean, Int> = TemplateDatabaseHelper.getDao(TemplateBean::class.java)

    override fun queryKeyword(kw: String): List<TemplateBean> {
        val builder = templateDao.queryBuilder()

        if (kw.isNotEmpty()) {
            builder.where()
                    .like(TemplateBean.TITLE, "%$kw%")
                    .or()
                    .like(TemplateBean.CONTENT, "%$kw%")
        }

        builder.orderBy(TemplateBean.UPDATE_TS, false)
        return builder.query()
    }

    override fun get(id: Int): TemplateBean? {
        return templateDao.queryForId(id)
    }
}